#-*- coding: utf-8 -*-

import re

header = u"""#This file is part of pyLot library.
#
# %(program)s is free software: you can redistribute it and/or modify
# it under the terms of the %(gnu_license)s as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# %(program)s is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# %(gnu_license)s for more details.
#
# You should have received a copy of the %(gnu_license)s
# along with %(program)s.  If not, see <http://www.gnu.org/licenses/>.

__author__ = %(__author__)r
__copyright__ = %(__copyright__)r
__credits__ = %(__credits__)r
__license__ = "%(gnu_license)s"

""" % dict(
  gnu_license=u'GNU Lesser General Public License',
  program=u'pyLot',
  __author__=u"Pierre Puiseux, Guillaume Baty",
  __copyright__=u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour",
  __credits__=[u"Pierre Puiseux", u"Guillaume Baty"],
  )

print header
import codecs

from pyLot.core import Path

co_encoding = re.compile(r'\#\s*\-\*\-\s*coding(\:|\=)\s*(utf|UTF)\-?8.*')
# valid for coding: utf-8, coding=UTF8, coding: UTF-8, ...
co_all_list = re.compile(r'__all__\s*=\s*')

def fix_encoding(pyfile):
  f = open(pyfile, 'r')

  encoding_declared = False
  for i in range(5):
    l = f.readline()
    if co_encoding.match(l) :
      encoding_declared = True
      break
  f.close()

  if encoding_declared :
    print 'OK', pyfile
  else :
    print '**ERROR**', pyfile,

    try :
      f = open(pyfile, 'r')
      lines = f.readlines()
      f.close()
      lines.insert(0, '# -*- coding: utf-8 -*-\n')
      f = open(pyfile, 'w')
      f.writelines(lines)
      f.close()
    except :
      print
    else :
      print '... FIXED !'

def fix_license(pyfile):
  f = codecs.open(pyfile, 'r', 'utf-8')
  lines = f.readlines()
  f.close()

  encoding_declared = False
  license_declared = False

  for i in range(min(len(lines), 5)):
    l = lines[i]
    if co_encoding.match(l) :
      encoding_declared = True
      break
  end_encoding = i + 1

  for i in range(min(len(lines), 20)):
    l = lines[i]
    if u'WITHOUT ANY WARRANTY' in l :
      license_declared = True
      break

  if encoding_declared and not license_declared :
    print 'maj licence pour', pyfile
    for i in range(end_encoding, len(lines)) :
      l = lines[i].strip()
      if not l:
        #première ligne vide après déclaration d'encodage
        break
    lines.insert(i + 1, header)

    f = codecs.open(pyfile, 'w', 'utf-8')
    f.writelines(lines)
    f.close()


def check_all_list(pyfile):
  exluded_pattern = [u'__init__.py', u'test_', u'api.py']
  for pattern in exluded_pattern :
    if pyfile.name.startswith(pattern):
      return

  f = codecs.open(pyfile, 'r', 'utf-8')
  lines = f.readlines()
  f.close()

  all_list_directive = False
  for i in range(min(len(lines), 100)):
    l = lines[i]
    if co_all_list.match(l):
      all_list_directive = True
      break

  if not all_list_directive :
    print pyfile, "ne semble pas contenir de directive __all__"


for pyfile in Path(u'.').walkfiles(u'*.py'):
  check_all_list(pyfile)
#  fix_license(pyfile)
#  fix_encoding(pyfile)

